package com.xiaoran.springboot3.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

//文件上传测试
@Controller
@Slf4j
public class FormTestController  {
    @GetMapping("/form_layouts")
    public String form_layouts(){
        return "form/form_layouts";
    }

    @PostMapping("/upload")
    public  String upload(@RequestParam("email") String email,
                          @RequestParam("username") String username,
                          @RequestPart("headering") MultipartFile headering,
                          @RequestPart("photos") MultipartFile[] photos) throws IOException {
        log.error("获取到我们上传到数据：email={},username={},heading={},photo={}",email,username,headering.getSize(),photos.length);
        //我们获取到了文件等信息，怎么保存
        //判断我们传入的文件是否为空：isEmpty
        if(!headering.isEmpty()){
            //获取到文件之前的名字
            String name=headering.getOriginalFilename();
            //我们获取到文件存入到磁盘文件哪
            headering.transferTo(new File("C:\\Users\\kkdjk\\Desktop\\xx\\x1\\"+name));
        }
        if(photos.length>0){
            for (MultipartFile file:photos){
                if (!file.isEmpty()){
                    //获取到文件之前名字
                    String name= file.getOriginalFilename();
                    file.transferTo(new File("C:\\Users\\kkdjk\\Desktop\\xx\\x2\\"+name));
                }
            }
        }
        return "main";
    }
}
